package com.cpd.blog.config;

import com.cpd.blog.common.constant.RedisKey;
import net.javacrumbs.shedlock.core.LockProvider;
import net.javacrumbs.shedlock.provider.redis.spring.RedisLockProvider;
import net.javacrumbs.shedlock.spring.annotation.EnableSchedulerLock;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.util.Objects;

/**
 * ShedLock 配置类，默认最大持锁 6分钟
 *
 * @Author: zeng_zeyu
 * @Date: 2022/01/17/15:34
 */
@Configuration
@EnableScheduling
@EnableSchedulerLock(defaultLockAtMostFor = "${schedule.defaultMaxLockTime:PT6M}")
public class ShedLockConfig {

    @Bean
    public LockProvider lockProvider(StringRedisTemplate redisTemplate) {
        return new RedisLockProvider(Objects.requireNonNull(redisTemplate.getConnectionFactory()),
                "", RedisKey.SHED_LOCK);
    }
}